<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="Lawted&#39;s Blog">
<meta property="og:url" content="http://example.com/page/5/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Lawted Wu">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/page/5/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'en'
  };
</script>
<title>Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/23/JavaScript/%E9%97%AD%E5%8C%85closure/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/23/JavaScript/%E9%97%AD%E5%8C%85closure/" class="post-title-link" itemprop="url">闭包closure</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-23 14:19:24" itemprop="dateCreated datePublished" datetime="2021-03-23T14:19:24+08:00">2021-03-23</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:29:21" itemprop="dateModified" datetime="2021-05-07T09:29:21+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h2><p>背景</p>
<ul>
<li>函数的执行依赖于变量作用域链，作用域链是在函数定义的时候决定的，而不是函数调用的时候决定的</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> scope = <span class="string">&quot;global scope&quot;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkscope</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> scope = <span class="string">&quot;local scope&quot;</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      	<span class="keyword">return</span> scope;</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="keyword">return</span> f();</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(checkscope()) <span class="comment">//&quot;local scope&quot;</span></span><br></pre></td></tr></table></figure>


<p>checkscope()函数现在返回的是一个函数对象，而不是一个函数结果，但是<strong>“作用域链是在函数定义的时候决定的，而不是函数调用的时候决定的”</strong>,所以结果依旧是局部变量，就像局部变量之前被存起来了一样，没有被回收掉</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> scope = <span class="string">&quot;global scope&quot;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkscope</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> scope = <span class="string">&quot;local scope&quot;</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      	<span class="keyword">return</span> scope;</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="keyword">return</span> f;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(checkscope()()) <span class="comment">//&quot;local scope&quot;</span></span><br></pre></td></tr></table></figure>


<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">constfuncs</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> funcs = [];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i =<span class="number">0</span>;i&lt;<span class="number">10</span>;i++)&#123;</span><br><span class="line">        funcs[i] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="keyword">return</span> i;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> funcs;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> funcs = constfuncs();</span><br><span class="line">funcs[<span class="number">5</span>]();     <span class="comment">//返回10</span></span><br></pre></td></tr></table></figure>
<p>一共创建了10个闭包，并将他们存储到一个数组中，所有的闭包都共享这一个i，所以结果是10</p>
<p>如果想获得正常的结果，需要将var i=0；改为let i = 0;因为let是块级作用域，每个i都不一样，10个闭包不能共享</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/23/JavaScript/concat-%E5%92%8Creduce/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/23/JavaScript/concat-%E5%92%8Creduce/" class="post-title-link" itemprop="url">concat()和reduce()</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-23 10:16:20" itemprop="dateCreated datePublished" datetime="2021-03-23T10:16:20+08:00">2021-03-23</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:29:37" itemprop="dateModified" datetime="2021-05-07T09:29:37+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h2><p>用来连接两个或者多个数组，不会改变现有的数组，会返回一个数组的副本</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;script type=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="built_in">document</span>.write(a.concat(<span class="number">4</span>,<span class="number">5</span>)); <span class="comment">//[1,2,3,4,5]</span></span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p>concat两个数组</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;script type=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">3</span>)</span><br><span class="line">arr[<span class="number">0</span>] = <span class="string">&quot;George&quot;</span></span><br><span class="line">arr[<span class="number">1</span>] = <span class="string">&quot;John&quot;</span></span><br><span class="line">arr[<span class="number">2</span>] = <span class="string">&quot;Thomas&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr2 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">3</span>)</span><br><span class="line">arr2[<span class="number">0</span>] = <span class="string">&quot;James&quot;</span></span><br><span class="line">arr2[<span class="number">1</span>] = <span class="string">&quot;Adrew&quot;</span></span><br><span class="line">arr2[<span class="number">2</span>] = <span class="string">&quot;Martin&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.write(arr.concat(arr2))</span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="reduce"><a href="#reduce" class="headerlink" title="reduce()"></a>reduce()</h2><p>一个累加器</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">array.reduce(function(total, currentValue, currentIndex, arr), initialValue)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>].reduce(<span class="function">(<span class="params">accumulator, currentValue, currentIndex, array</span>) =&gt;</span> </span><br><span class="line">       &#123; <span class="keyword">return</span> accumulator + currentValue; &#125;, <span class="number">10</span> );</span><br><span class="line"><span class="comment">// 提供初始值为 10,最后累加为20</span></span><br></pre></td></tr></table></figure>
<ul>
<li>累加对象里的值</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> sum = [&#123;<span class="attr">x</span>: <span class="number">1</span>&#125;, &#123;<span class="attr">x</span>:<span class="number">2</span>&#125;, &#123;<span class="attr">x</span>:<span class="number">3</span>&#125;].reduce(</span><br><span class="line">    (accumulator, currentValue) =&gt; accumulator + currentValue.x</span><br><span class="line">    ,<span class="number">0</span></span><br><span class="line">);</span><br><span class="line"> </span><br><span class="line"><span class="built_in">console</span>.log(sum) <span class="comment">// logs 6</span></span><br></pre></td></tr></table></figure>
<ul>
<li>计算元素出现的次数</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> names = [<span class="string">&#x27;Alice&#x27;</span>, <span class="string">&#x27;Bob&#x27;</span>, <span class="string">&#x27;Tiff&#x27;</span>, <span class="string">&#x27;Bruce&#x27;</span>, <span class="string">&#x27;Alice&#x27;</span>];</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> countedNames = names.reduce(<span class="function"><span class="keyword">function</span> (<span class="params">allNames, name</span>) </span>&#123; </span><br><span class="line">  <span class="keyword">if</span> (name <span class="keyword">in</span> allNames) &#123;</span><br><span class="line">    allNames[name]++;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">    allNames[name] = <span class="number">1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> allNames;</span><br><span class="line">&#125;, &#123;&#125;);</span><br><span class="line"><span class="comment">// countedNames is:</span></span><br><span class="line"><span class="comment">// &#123; &#x27;Alice&#x27;: 2, &#x27;Bob&#x27;: 1, &#x27;Tiff&#x27;: 1, &#x27;Bruce&#x27;: 1 &#125;</span></span><br></pre></td></tr></table></figure>


<h4 id="结合reduce-和contact-来进行数组扁平化"><a href="#结合reduce-和contact-来进行数组扁平化" class="headerlink" title="结合reduce()和contact()来进行数组扁平化"></a>结合reduce()和contact()来进行数组扁平化</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,[<span class="number">2</span>,[<span class="number">3</span>,[<span class="number">4</span>,<span class="number">5</span>],<span class="number">6</span>]]];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flatten</span> (<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arr.reduce(<span class="function">(<span class="params">pre, item</span>) =&gt;</span> <span class="built_in">Array</span>.isArray(item) ? pre.concat(flatten(item)) : pre.concat(item) , [])</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(flatten(arr));  <span class="comment">//[1, 2, 3, 4, 5, 6]</span></span><br></pre></td></tr></table></figure>









      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/23/JavaScript/%E6%95%B0%E7%BB%84%E6%89%81%E5%B9%B3%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/23/JavaScript/%E6%95%B0%E7%BB%84%E6%89%81%E5%B9%B3%E5%8C%96/" class="post-title-link" itemprop="url">数组扁平化</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-23 10:15:22" itemprop="dateCreated datePublished" datetime="2021-03-23T10:15:22+08:00">2021-03-23</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:29:29" itemprop="dateModified" datetime="2021-05-07T09:29:29+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h3 id="ES6-flat-depth"><a href="#ES6-flat-depth" class="headerlink" title="ES6 flat(depth)"></a>ES6 flat(depth)</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> a = [<span class="number">1</span>,[<span class="number">2</span>,<span class="number">3</span>]];  </span><br><span class="line">a.flat(); <span class="comment">// [1,2,3]  </span></span><br><span class="line">a.flat(<span class="number">1</span>); <span class="comment">//[1,2,3]</span></span><br></pre></td></tr></table></figure>
<p>可以使用 flat(Infinity)来把数组直接降至一维</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> a = [<span class="number">1</span>,[<span class="number">2</span>,<span class="number">3</span>,[<span class="number">4</span>,[<span class="number">5</span>]]]];  </span><br><span class="line"><span class="built_in">console</span>.log(a.flat(<span class="literal">Infinity</span>))</span><br><span class="line"><span class="comment">//不会改变原数组，需要重新赋值打印</span></span><br></pre></td></tr></table></figure>


<h3 id="循环递归方式实现"><a href="#循环递归方式实现" class="headerlink" title="循环递归方式实现"></a>循环递归方式实现</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flatten</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> rets = [];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(arr[i])) &#123;</span><br><span class="line">            rets = rets.concat(flatten(arr[i]));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            rets.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> rets;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以使用concat + reduce</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flatten</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> arr.reduce(<span class="function">(<span class="params">pre,item</span>) =&gt;</span> <span class="built_in">Array</span>.isArray(item)?pre.concat(flatten(item)):pre.concat(item),[])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/22/JavaScript/call()%E5%92%8Capply()/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/22/JavaScript/call()%E5%92%8Capply()/" class="post-title-link" itemprop="url">call() apply() bind()</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-22 17:52:47" itemprop="dateCreated datePublished" datetime="2021-03-22T17:52:47+08:00">2021-03-22</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:29:46" itemprop="dateModified" datetime="2021-05-07T09:29:46+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="call"><a href="#call" class="headerlink" title="call()"></a>call()</h2><p>调用另一个对象的方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">猫吃鱼，狗吃肉</span><br><span class="line">猫.吃鱼.call(狗，鱼) 来让狗吃鱼</span><br><span class="line">狗.吃肉.call(猫，肉) 来让猫吃肉</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">    fullName: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.firstName + <span class="string">&quot; &quot;</span> + <span class="built_in">this</span>.lastName;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> person1 = &#123;</span><br><span class="line">    firstName:<span class="string">&quot;Bill&quot;</span>,</span><br><span class="line">    lastName: <span class="string">&quot;Gates&quot;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> person2 = &#123;</span><br><span class="line">    firstName:<span class="string">&quot;Steve&quot;</span>,</span><br><span class="line">    lastName: <span class="string">&quot;Jobs&quot;</span>,</span><br><span class="line">&#125;</span><br><span class="line">person.fullName.call(person1);  <span class="comment">// 将返回 &quot;Bill Gates&quot;</span></span><br></pre></td></tr></table></figure>
<p>带参数的情况</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">  fullName: <span class="function"><span class="keyword">function</span>(<span class="params">city, country</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.firstName + <span class="string">&quot; &quot;</span> + <span class="built_in">this</span>.lastName + <span class="string">&quot;,&quot;</span> + city + <span class="string">&quot;,&quot;</span> + country;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> person1 = &#123;</span><br><span class="line">  firstName:<span class="string">&quot;John&quot;</span>,</span><br><span class="line">  lastName: <span class="string">&quot;Doe&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">person.fullName.apply(person1, <span class="string">&quot;Oslo&quot;</span>, <span class="string">&quot;Norway&quot;</span>);</span><br></pre></td></tr></table></figure>


<h2 id="Apply"><a href="#Apply" class="headerlink" title="Apply()"></a>Apply()</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">    fullName: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.firstName + <span class="string">&quot; &quot;</span> + <span class="built_in">this</span>.lastName;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> person1 = &#123;</span><br><span class="line">    firstName: <span class="string">&quot;Bill&quot;</span>,</span><br><span class="line">    lastName: <span class="string">&quot;Gates&quot;</span>,</span><br><span class="line">&#125;</span><br><span class="line">person.fullName.apply(person1);  </span><br></pre></td></tr></table></figure>
<p>带参数的情况</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">  fullName: <span class="function"><span class="keyword">function</span>(<span class="params">city, country</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.firstName + <span class="string">&quot; &quot;</span> + <span class="built_in">this</span>.lastName + <span class="string">&quot;,&quot;</span> + city + <span class="string">&quot;,&quot;</span> + country;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> person1 = &#123;</span><br><span class="line">  firstName:<span class="string">&quot;John&quot;</span>,</span><br><span class="line">  lastName: <span class="string">&quot;Doe&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">person.fullName.apply(person1, [<span class="string">&quot;Oslo&quot;</span>, <span class="string">&quot;Norway&quot;</span>]);</span><br></pre></td></tr></table></figure>


<p><strong>call() 方法分别接受参数。</strong></p>
<p><strong>apply() 方法接受数组形式的参数。</strong></p>
<h2 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h2><p>将数组 arr 中的元素作为调用函数 fn 的参数</p>
<p>示例1</p>
<h2 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function (greeting, name, punctuation) &#123;return greeting + &#39;, &#39; + name + (punctuation || &#39;!&#39;);&#125;, [&#39;Hello&#39;, &#39;Ellie&#39;, &#39;!&#39;]</span><br></pre></td></tr></table></figure>
<h2 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Hello, Ellie!</span><br></pre></td></tr></table></figure>


<h4 id="使用call"><a href="#使用call" class="headerlink" title="使用call()"></a>使用call()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">argsAsArray</span>(<span class="params">fn, arr</span>) </span>&#123;    </span><br><span class="line">    <span class="keyword">return</span> fn.call(fn,arr[<span class="number">0</span>],arr[<span class="number">1</span>],arr[<span class="number">2</span>])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="使用apply"><a href="#使用apply" class="headerlink" title="使用apply()"></a>使用apply()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">argsAsArray</span>(<span class="params">fn, arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> fn.apply(fn,arr)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="bind"><a href="#bind" class="headerlink" title="bind()"></a>bind()</h2><p>和call() , apply()类似，但是返回的是函数 需要手动调用</p>
<h2 id="Call-apply-bind-修改this指向"><a href="#Call-apply-bind-修改this指向" class="headerlink" title="Call() apply() bind()修改this指向"></a>Call() apply() bind()修改this指向</h2><p>将函数 fn 的执行上下文改为 obj 对象</p>
<h3 id="call-1"><a href="#call-1" class="headerlink" title="call()"></a>call()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">speak</span>(<span class="params">fn,obj</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> fn.call(obj)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="apply"><a href="#apply" class="headerlink" title="apply()"></a>apply()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">speak</span>(<span class="params">fn, obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> fn.apply(obj)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="bind-1"><a href="#bind-1" class="headerlink" title="bind()"></a>bind()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">speak</span>(<span class="params">fn, obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> b = fn.bind(obj)</span><br><span class="line">    <span class="keyword">return</span> b()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">speak</span>(<span class="params">fn, obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> fn.bind(obj)()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/22/JavaScript/setTimeout-%E5%92%8CsetInterval/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/22/JavaScript/setTimeout-%E5%92%8CsetInterval/" class="post-title-link" itemprop="url">setTimeout()和setInterval()</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-22 17:03:23" itemprop="dateCreated datePublished" datetime="2021-03-22T17:03:23+08:00">2021-03-22</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:29:55" itemprop="dateModified" datetime="2021-05-07T09:29:55+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="setTimeout"><a href="#setTimeout" class="headerlink" title="setTimeout"></a>setTimeout</h2><p>setTimeout()是实现一个函数在指定的毫秒数以后运行,只运行一次，逻辑是告诉浏览器会在200s后插入一段代码，但是如果之前的代码运行了300秒，就并不会在200s的时候运行，而是200秒的时候插入，300s后线程闲置才会运行100s前插入的代码</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; </span><br><span class="line">    alert(<span class="string">&quot;Hello&quot;</span>); </span><br><span class="line">&#125;, <span class="number">3000</span>);</span><br></pre></td></tr></table></figure>
<p>可以通过调用clearTimeout()来停止</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    myVar = <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="string">&quot;Hello&quot;</span>) &#125;, <span class="number">3000</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myStopFunction</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">clearTimeout</span>(myVar);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="setInterval"><a href="#setInterval" class="headerlink" title="setInterval()"></a>setInterval()</h2><p>setInterval()是实现会在指定的毫秒间隔里面重复调用函数，直到clearInterval()被调用才会停止</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//实现一个动态进度条</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> elem = <span class="built_in">document</span>.getElementById(<span class="string">&quot;myBar&quot;</span>); </span><br><span class="line">  <span class="keyword">var</span> width = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">var</span> id = <span class="built_in">setInterval</span>(frame, <span class="number">10</span>);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">frame</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (width == <span class="number">100</span>) &#123;</span><br><span class="line">      <span class="built_in">clearInterval</span>(id);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      width++; </span><br><span class="line">      elem.style.width = width + <span class="string">&#x27;%&#x27;</span>; </span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




<h3 id="类似eval-的特性"><a href="#类似eval-的特性" class="headerlink" title="类似eval()的特性"></a>类似eval()的特性</h3><p>setTimeout()和setInterval()的第一个参数都可以是字符串，相当于eval(),直接执行字符串</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">eval</span>(<span class="string">&quot;2+3&quot;</span>)	<span class="comment">// 返回 5</span></span><br></pre></td></tr></table></figure>


<ul>
<li>实现一个打点计时器，要求<br>1、从 start 到 end（包含 start 和 end），每隔 100 毫秒 console.log 一个数字，每次数字增幅为 1<br>2、返回的对象中需要包含一个 cancel 方法，用于停止定时操作<br>3、第一个数需要立即输出</li>
</ul>
<h4 id="使用setInterval"><a href="#使用setInterval" class="headerlink" title="使用setInterval()"></a>使用setInterval()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">count</span>(<span class="params">start, end</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(start++)</span><br><span class="line">    <span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(start&lt;=end)&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(start++)</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">clearInterval</span>(timer);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,<span class="number">100</span>)</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        cancel: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">clearInterval</span>(timer)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="使用setTimeout"><a href="#使用setTimeout" class="headerlink" title="使用setTimeout()"></a>使用setTimeout()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">count</span>(<span class="params">start, end</span>) </span>&#123;</span><br><span class="line">   <span class="keyword">if</span>(start&lt;=end)&#123;</span><br><span class="line">       <span class="built_in">console</span>.log(start)</span><br><span class="line">       start++</span><br><span class="line">       st = <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">           count(start,end),<span class="number">100</span></span><br><span class="line">       &#125;)</span><br><span class="line">   &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        cancel: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">clearTimeout</span>(st)&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/18/JavaScript/slice(0)%E7%9A%84%E7%94%A8%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/03/18/JavaScript/slice(0)%E7%9A%84%E7%94%A8%E6%B3%95/" class="post-title-link" itemprop="url">slice(0)的用法和splice的返回值</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-03-18 20:49:48" itemprop="dateCreated datePublished" datetime="2021-03-18T20:49:48+08:00">2021-03-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:30:14" itemprop="dateModified" datetime="2021-05-07T09:30:14+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="slice-0-可以用来拷贝数组"><a href="#slice-0-可以用来拷贝数组" class="headerlink" title="slice(0)可以用来拷贝数组"></a>slice(0)可以用来拷贝数组</h2><p>在数组 arr 的 index 处添加元素 item。不要直接修改数组 arr，结果返回新的数组</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function insert(arr, item, index) &#123;</span><br><span class="line">    var brr &#x3D; arr.slice(0)</span><br><span class="line">    brr.splice(index,0,item)</span><br><span class="line">    return brr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>通过arr.silce(0)的方法来拷贝数组</strong></p>
<h2 id="splice的返回值"><a href="#splice的返回值" class="headerlink" title="splice的返回值"></a>splice的返回值</h2><p>在代码中可以看到使用splice的时候不需要重新赋值给brr，<strong>因为splice返回的是被删除的元素</strong>，反而会出错</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/19/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/02/19/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" class="post-title-link" itemprop="url">正则表达式</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-02-19 11:19:55" itemprop="dateCreated datePublished" datetime="2021-02-19T11:19:55+08:00">2021-02-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-04-19 14:49:18" itemprop="dateModified" datetime="2021-04-19T14:49:18+08:00">2021-04-19</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h1><h3 id=""><a href="#" class="headerlink" title="."></a>.</h3><p>匹配任意字符</p>
<h3 id="-1"><a href="#-1" class="headerlink" title="^"></a>^</h3><p>匹配字符串开始位置</p>
<h3 id="-2"><a href="#-2" class="headerlink" title="$"></a>$</h3><p>匹配字符串结束位置</p>
<hr>
<h3 id="w"><a href="#w" class="headerlink" title="\w"></a>\w</h3><p>匹配字母和数字</p>
<h3 id="W"><a href="#W" class="headerlink" title="\W"></a>\W</h3><p>匹配非字母和非数字</p>
<hr>
<h3 id="d"><a href="#d" class="headerlink" title="\d"></a>\d</h3><p>匹配数字</p>
<h3 id="D"><a href="#D" class="headerlink" title="\D"></a>\D</h3><p>匹配非数字</p>
<hr>
<h3 id="s"><a href="#s" class="headerlink" title="\s"></a>\s</h3><p>匹配空格</p>
<h3 id="S"><a href="#S" class="headerlink" title="\S"></a>\S</h3><p>匹配非空格</p>
<hr>
<h3 id="b"><a href="#b" class="headerlink" title="\b"></a>\b</h3><p>匹配一个单词边界，即字与空格间的位置。</p>
<h3 id="B"><a href="#B" class="headerlink" title="\B"></a>\B</h3><p> 非单词边界匹配。</p>
<hr>
<h3 id="-3"><a href="#-3" class="headerlink" title="[ ]"></a>[ ]</h3><p>匹配里面的任意字符</p>
<p>[a-zA-Z0-9]</p>
<h3 id="-4"><a href="#-4" class="headerlink" title="[^ ]"></a>[^ ]</h3><p>匹配不在里面的字符</p>
<hr>
<h3 id="-5"><a href="#-5" class="headerlink" title="+"></a>+</h3><p>匹配一次或多次</p>
<h3 id="-6"><a href="#-6" class="headerlink" title="*"></a>*</h3><p>匹配零次或多次</p>
<h3 id="？"><a href="#？" class="headerlink" title="？"></a>？</h3><p>匹配零次或一次</p>
<hr>
<h3 id="-7"><a href="#-7" class="headerlink" title="|"></a>|</h3><p>或者</p>
<h3 id="-8"><a href="#-8" class="headerlink" title="( )"></a>( )</h3><p>分组</p>
<h3 id="-9"><a href="#-9" class="headerlink" title="{ }"></a>{ }</h3><p>匹配个数；{2}表示匹配2次，{4，8}匹配4到8次</p>
<hr>
<h3 id="-10"><a href="#-10" class="headerlink" title="?:"></a>?:</h3><p>不捕获分组中的</p>
<hr>
<h3 id="-11"><a href="#-11" class="headerlink" title="?="></a>?=</h3><p>正向预查</p>
<h3 id="-12"><a href="#-12" class="headerlink" title="?!"></a>?!</h3><p>正向否定预查</p>
<hr>
<h3 id="lt"><a href="#lt" class="headerlink" title="?&lt;="></a>?&lt;=</h3><p>反向预查</p>
<h3 id="-13"><a href="#-13" class="headerlink" title="?!="></a>?!=</h3><p>反向否定预查</p>
<hr>
<h3 id="1"><a href="#1" class="headerlink" title="\1"></a>\1</h3><p>匹配第一个分组的内容</p>
<h3 id="2"><a href="#2" class="headerlink" title="\2"></a>\2</h3><p>匹配第二个分组的内容</p>
<hr>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/08/JavaScript/%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/02/08/JavaScript/%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95-1/" class="post-title-link" itemprop="url">数组方法</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-02-08 16:57:36" itemprop="dateCreated datePublished" datetime="2021-02-08T16:57:36+08:00">2021-02-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:30:28" itemprop="dateModified" datetime="2021-05-07T09:30:28+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="数组方法"><a href="#数组方法" class="headerlink" title="数组方法"></a>数组方法</h1><h3 id="join"><a href="#join" class="headerlink" title="join()"></a>join()</h3><p>Array.join()将数组中所有的元素都转化为字符串并拼接在一起，返回生成的字符串，空格中选择隔开的方式</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">a = a.join(<span class="string">&quot;-&quot;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//1-2-3-4</span></span><br></pre></td></tr></table></figure>
<p>Array.join() 是String.split()方法的逆向操作，String.split()是将字符串分割成若干块来创建数组</p>
<h3 id="reverse"><a href="#reverse" class="headerlink" title="reverse()"></a>reverse()</h3><p>返回倒序数组</p>
<h3 id="sort"><a href="#sort" class="headerlink" title="sort()"></a>sort()</h3><p>如果无参数，将会按照字母表的顺序排序，可以在括号中传递一个比较函数，来决定先后顺序</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">33</span>,<span class="number">4</span>,<span class="number">1111</span>,<span class="number">222</span>,<span class="number">55555</span>]</span><br><span class="line">a.sort()</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//字母表排序:1111，222，33，4，55555</span></span><br><span class="line"></span><br><span class="line">a = a.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a-b;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//数值排序:4, 33, 222, 1111, 55555</span></span><br><span class="line"></span><br><span class="line">a = a.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> b-a;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//从大到小排序:55555, 1111, 222, 33, 4</span></span><br></pre></td></tr></table></figure>


<h3 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h3><p>拼接数组</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">a = a.concat(<span class="number">4</span>,<span class="number">5</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br></pre></td></tr></table></figure>


<h3 id="slice"><a href="#slice" class="headerlink" title="slice()"></a>slice()</h3><p>返回指定数组的一个片段或子数组，两个参数分别指定了开始和结束的位置，负数就是倒数，最后一个是倒数第1个，可以用slice(-1)，-3指定了倒数第三个，a,b两个参数表示，[a，b)</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">a = a.slice(<span class="number">1</span>,-<span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//[ 2, 3 ]</span></span><br></pre></td></tr></table></figure>


<h3 id="splice"><a href="#splice" class="headerlink" title="splice()"></a>splice()</h3><p>splice(a,b,c,d,e)表示，从a的地方，删除b个数，返回删去以后的，然后把c,d,e添加到数组的a位置，如果b的值为0那么就从a删到末尾</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>]</span><br><span class="line">a.splice(<span class="number">2</span>,<span class="number">3</span>,<span class="number">70</span>,<span class="number">80</span>);</span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br><span class="line"><span class="comment">//1,2,70,80,6,7,8</span></span><br></pre></td></tr></table></figure>


<h3 id="push-和pop"><a href="#push-和pop" class="headerlink" title="push()和pop()"></a>push()和pop()</h3><p>类似于栈</p>
<h3 id="unshift-和shift"><a href="#unshift-和shift" class="headerlink" title="unshift()和shift()"></a>unshift()和shift()</h3><p>与push()和pop()相反，进行头部分操作</p>
<h2 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString()"></a>toString()</h2><p>转换为字符串和join()返回的一样</p>
<h2 id="ES5新增"><a href="#ES5新增" class="headerlink" title="ES5新增"></a>ES5新增</h2><h3 id="foreach"><a href="#foreach" class="headerlink" title="foreach()"></a>foreach()</h3><p>为每个元素调用指定的函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> data = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line">data.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">v, i, a</span>) </span>&#123; a[i] = v + <span class="number">1</span> &#125;)</span><br><span class="line"><span class="built_in">console</span>.log(data);</span><br></pre></td></tr></table></figure>
<p>注意foreach没有break，如果要提前终止，需要把foreach()方法放在一个try块中，并且抛出一个异常</p>
<h3 id="map"><a href="#map" class="headerlink" title="map()"></a>map()</h3><p>map()方法将调用的数组的每个元素传递给指定的函数，然后返回一个数组，类似于映射</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">b = a.map(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;<span class="keyword">return</span> x*x&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(b);<span class="comment">//[1,4,9]</span></span><br></pre></td></tr></table></figure>


<h3 id="filter"><a href="#filter" class="headerlink" title="filter()"></a>filter()</h3><p>过滤器</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">smallone = a.filter(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;<span class="keyword">return</span> x&lt;<span class="number">3</span>&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(smallone);</span><br></pre></td></tr></table></figure>


<h3 id="every-和some"><a href="#every-和some" class="headerlink" title="every()和some()"></a>every()和some()</h3><p>返回true/false</p>
<p>every类似于数学中的所有</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a.every(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;<span class="keyword">return</span> x&lt;<span class="number">10</span>;&#125;));<span class="comment">//数组a中所有都小于10</span></span><br></pre></td></tr></table></figure>
<p>some类似于数学中的存在</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a.some(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;<span class="keyword">return</span> x%<span class="number">2</span>===<span class="number">0</span>&#125;));<span class="comment">//数组a中存在偶数</span></span><br></pre></td></tr></table></figure>


<h3 id="reduce-和reduceRight"><a href="#reduce-和reduceRight" class="headerlink" title="reduce()和reduceRight()"></a>reduce()和reduceRight()</h3><p>reduce需要一个函数，和两个参数，还有一个初始值，x为上一次计算出来的值，y为从数组中获取的值</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">sum = a.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">x,y</span>)</span>&#123;<span class="keyword">return</span> x+y&#125;,<span class="number">0</span>)</span><br><span class="line">product = a.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">x,y</span>)</span>&#123;<span class="keyword">return</span> x*y&#125;,<span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(sum);<span class="comment">//求和</span></span><br><span class="line"><span class="built_in">console</span>.log(product);<span class="comment">//求积</span></span><br></pre></td></tr></table></figure>
<p>reduceRight的操作是从右到左</p>
<h3 id="indexOf-和lastIndexOf"><a href="#indexOf-和lastIndexOf" class="headerlink" title="indexOf()和lastIndexOf()"></a>indexOf()和lastIndexOf()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a.indexOf(<span class="number">2</span>));<span class="comment">//1 because a[1]=2</span></span><br><span class="line"><span class="built_in">console</span>.log(a.lastIndexOf(<span class="number">2</span>));<span class="comment">//5 because a[5]=2反向</span></span><br></pre></td></tr></table></figure>
<p>他们也有第二个参数，可以指定开始搜索的索引</p>
<h3 id="Array-isArray"><a href="#Array-isArray" class="headerlink" title="Array.isArray()"></a>Array.isArray()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> isArray = <span class="built_in">Function</span>.isArray||<span class="function"><span class="keyword">function</span>(<span class="params">o</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> o === <span class="string">&quot;object&quot;</span> &amp;&amp;</span><br><span class="line">    <span class="built_in">Object</span>.prototype.toString.call(o) === <span class="string">&quot;[object Array]&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/08/%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%89%E4%B8%AA%E5%B1%9E%E6%80%A7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/02/08/%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%89%E4%B8%AA%E5%B1%9E%E6%80%A7/" class="post-title-link" itemprop="url">对象的三个属性</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>
      

      <time title="Created: 2021-02-08 11:58:39 / Modified: 14:35:39" itemprop="dateCreated datePublished" datetime="2021-02-08T11:58:39+08:00">2021-02-08</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="对象的三个属性"><a href="#对象的三个属性" class="headerlink" title="对象的三个属性"></a>对象的三个属性</h2><p>每个对象都有三个属性</p>
<ul>
<li>原型(prototype)</li>
<li>类(class)</li>
<li>可拓展性(extensible attribute)</li>
</ul>
<h3 id="原型属性"><a href="#原型属性" class="headerlink" title="原型属性"></a>原型属性</h3><p>要检测一个对象是否是另一个对象的原型，可以使用isPrototypeOf()方法，类似instanceof</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> p = &#123;<span class="attr">x</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="keyword">var</span> o = <span class="built_in">Object</span>.create(p)</span><br><span class="line"><span class="built_in">console</span>.log(p.isPrototypeOf(o));<span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.isPrototypeOf(p));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>


<h3 id="类属性"><a href="#类属性" class="headerlink" title="类属性"></a>类属性</h3><p>可以手写一个classof()函数，调用对象的toString()方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">classof</span>(<span class="params">o</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(o === <span class="literal">null</span>)<span class="keyword">return</span> <span class="string">&quot;Null&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span>(o === <span class="literal">undefined</span>)<span class="keyword">return</span> <span class="string">&quot;Undefined&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Object</span>.prototype.toString.call(o).slice(<span class="number">8</span>,-<span class="number">1</span>)<span class="comment">//因为是[object class]所以取第八个到倒数第二个</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="可拓展性"><a href="#可拓展性" class="headerlink" title="可拓展性"></a>可拓展性</h3>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/02/08/JavaScript/%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%89%E4%B8%AA%E5%B1%9E%E6%80%A7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/02/08/JavaScript/%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%89%E4%B8%AA%E5%B1%9E%E6%80%A7/" class="post-title-link" itemprop="url">对象的三个属性</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-02-08 11:58:39" itemprop="dateCreated datePublished" datetime="2021-02-08T11:58:39+08:00">2021-02-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:30:35" itemprop="dateModified" datetime="2021-05-07T09:30:35+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="对象的三个属性"><a href="#对象的三个属性" class="headerlink" title="对象的三个属性"></a>对象的三个属性</h2><p>每个对象都有三个属性</p>
<ul>
<li>原型(prototype)</li>
<li>类(class)</li>
<li>可拓展性(extensible attribute)</li>
</ul>
<h3 id="原型属性"><a href="#原型属性" class="headerlink" title="原型属性"></a>原型属性</h3><p>要检测一个对象是否是另一个对象的原型，可以使用isPrototypeOf()方法，类似instanceof</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> p = &#123;<span class="attr">x</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="keyword">var</span> o = <span class="built_in">Object</span>.create(p)</span><br><span class="line"><span class="built_in">console</span>.log(p.isPrototypeOf(o));<span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.isPrototypeOf(p));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>


<h3 id="类属性"><a href="#类属性" class="headerlink" title="类属性"></a>类属性</h3><p>可以手写一个classof()函数，调用对象的toString()方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">classof</span>(<span class="params">o</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(o === <span class="literal">null</span>)<span class="keyword">return</span> <span class="string">&quot;Null&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span>(o === <span class="literal">undefined</span>)<span class="keyword">return</span> <span class="string">&quot;Undefined&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Object</span>.prototype.toString.call(o).slice(<span class="number">8</span>,-<span class="number">1</span>)<span class="comment">//因为是[object class]所以取第八个到倒数第二个</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="可拓展性"><a href="#可拓展性" class="headerlink" title="可拓展性"></a>可拓展性</h3>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/4/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/4/">4</a><span class="page-number current">5</span><a class="page-number" href="/page/6/">6</a><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/6/"><i class="fa fa-angle-right" aria-label="Next page"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
